package com.fancy.learncenter.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.adapter.InviteAdapter;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.InviteDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.view.CustomFootView;
import com.fancy.learncenter.view.CustomHeaderView;
import com.fancy.learncenter.view.PopupWindowCity;
import com.fancy.learncenter.view.PopupWindowEducationType;
import com.fancy.learncenter.view.PopupWindowInviteTimeType;
import com.fancy.learncenter.view.PopupWindowSalaryType;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class InviteActivity extends BaseActivity {


    ArrayList<String> timeDatas;

    PopupWindowSalaryType salaryType;
    PopupWindowInviteTimeType popupWindowExperienceType;


    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.conditionView)
    LinearLayout conditionView;
    @Bind(R.id.localText)
    TextView localText;
    @Bind(R.id.localIcon)
    ImageView localIcon;
    @Bind(R.id.salaryText)
    TextView salaryText;
    @Bind(R.id.salaryIcon)
    ImageView salaryIcon;
    @Bind(R.id.timeText)
    TextView timeText;
    @Bind(R.id.timeIcon)
    ImageView timeIcon;
    @Bind(R.id.xRefreshView)
    XRefreshView xRefreshView;
    @Bind(R.id.rootView)
    LinearLayout rootView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_invite, getResources().getColor(R.color.toolbar_color));

        ButterKnife.bind(this);

        initToolbar("招聘");

        setRight("发布", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InviteActivity.this.startActivity(new Intent(  InviteActivity.this, ReleaseZhiWeiActivity.class));
            }
        });

        salaryType = new PopupWindowSalaryType(this, conditionView);
        salaryType.setClickCallBack(new PopupWindowEducationType.ClickCallBack() {
            @Override
            public void cancleCallBack() {

                salaryText.setTextColor(Color.parseColor("#5a6062"));
                salaryIcon.setImageResource(R.mipmap.ic_jiantouxia);
            }

            @Override
            public void selecCallBack(String content, String id) {
                salaryText.setText(content);

                salaryId = id;
                getDatas(true);
            }
        });

        popupWindowExperienceType = new PopupWindowInviteTimeType(this, conditionView);

        popupWindowExperienceType.setClickCallBack(new PopupWindowEducationType.ClickCallBack() {
            @Override
            public void cancleCallBack() {
                timeText.setTextColor(Color.parseColor("#5a6062"));
                timeIcon.setImageResource(R.mipmap.ic_jiantouxia);
            }

            @Override
            public void selecCallBack(String content, String id) {
                timeText.setText(content);

                timeId = id;
                getDatas(true);
            }
        });


        xRefreshView.setPullLoadEnable(true);

        timeDatas = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);

        adapter = new InviteAdapter(this, new ArrayList<InviteDataBean>());
        recycleView.setAdapter(adapter);

        adapter.setCustomLoadMoreView(new CustomFootView(this));
        xRefreshView.setPullLoadEnable(true);
        xRefreshView.setCustomHeaderView(new CustomHeaderView(this));
        xRefreshView.setPinnedTime(1000);

        xRefreshView.setXRefreshViewListener(new XRefreshView.XRefreshViewListener() {
            @Override
            public void onRefresh() {
                LogUtil.MyLog("onLoadMore", "====onRefresh==");
                getDatas(true);
            }

            @Override
            public void onRefresh(boolean isPullDown) {

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                LogUtil.MyLog("onLoadMore", "====onLoadMore==");
                getDatas(false);
            }

            @Override
            public void onRelease(float direction) {

            }

            @Override
            public void onHeaderMove(double headerMovePercent, int offsetY) {

            }
        });


        popupWindowCity = new PopupWindowCity(this);
        popupWindowCity.setSelectCallBack(new PopupWindowCity.SelectCallBack() {
            @Override
            public void selectCallBack(String province, String city, String area, String id) {
                localText.setText(area);

                locationId = id;
                getDatas(true);
            }


        });


        getDatas(true);

    }


    InviteAdapter adapter;
    PopupWindowCity popupWindowCity;

    @OnClick({R.id.local, R.id.salary, R.id.time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.local:
                popupWindowCity.ShowPickerView();
                salaryType.dismiss();
                popupWindowExperienceType.dismiss();
                selectCondition(1);
                break;
            case R.id.salary:
                popupWindowExperienceType.dismiss();
                salaryType.showPopupWindow();
                selectCondition(2);
                break;
            case R.id.time:
                salaryType.dismiss();
                popupWindowExperienceType.showPopupWindow();
                selectCondition(3);
                break;
        }
    }

    private void selectCondition(int index) {
        localText.setTextColor(Color.parseColor("#5a6062"));
        localIcon.setImageResource(R.mipmap.ic_jiantouxia);

        salaryText.setTextColor(Color.parseColor("#5a6062"));
        salaryIcon.setImageResource(R.mipmap.ic_jiantouxia);

        timeText.setTextColor(Color.parseColor("#5a6062"));
        timeIcon.setImageResource(R.mipmap.ic_jiantouxia);


        if (index == 1) {

            // localText.setTextColor(Color.parseColor("#ff8200"));

            // localIcon.setImageResource(R.mipmap.ic_jiantoushang);

        } else if (index == 2) {

            salaryText.setTextColor(Color.parseColor("#ff8200"));

            salaryIcon.setImageResource(R.mipmap.ic_jiantoushang);

        } else if (index == 3) {

            timeText.setTextColor(Color.parseColor("#ff8200"));

            timeIcon.setImageResource(R.mipmap.ic_jiantoushang);

        }
    }


    String locationId = "";

    String salaryId = "";

    String timeId = "";


    private int page = 0;

    private void getDatas(final boolean isRefresh) {
        Map map = new HashMap();

        if (isRefresh) {
            page = 0;
        } else {
            page++;
        }

        map.put("page", page + "");

        map.put("locationId", locationId);

        map.put("salary", salaryId);

        map.put("time", timeId);

        HttpMehtod.getInstance().inviteList(map, new IdeaObserver<BaseDataBean<List<InviteDataBean>>>(this, true) {
            @Override
            public void onSuccess(BaseDataBean<List<InviteDataBean>> listBaseDataBean) {

                if (isRefresh) {
                    xRefreshView.stopRefresh();
                    xRefreshView.setLoadComplete(false);
                    if (listBaseDataBean.getData() == null) {
                        adapter.notifyDataSetChanged(new ArrayList<InviteDataBean>());
                        return;
                    }
                    adapter.notifyDataSetChanged((ArrayList<InviteDataBean>) listBaseDataBean.getData());
                } else {
                    if (listBaseDataBean.getData() == null) {
                        xRefreshView.setLoadComplete(true);
                    } else if (listBaseDataBean.getData() != null && listBaseDataBean.getData().size() < Constant.PAGE_NUM) {
                        xRefreshView.setLoadComplete(true);
                    } else {
                        xRefreshView.stopLoadMore();
                    }


                    if (listBaseDataBean.getData() == null) {
                        return;
                    }
                    adapter.addAll(listBaseDataBean.getData());
                }
            }

            @Override
            public void onFail(int errorCode) {
                xRefreshView.stopRefresh();
                xRefreshView.setLoadComplete(false);
            }
        });

    }
}
